package net.lab1024.sa.admin.module.users.usage.service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import lombok.Data;
import net.lab1024.sa.admin.module.fabricmaterials.dao.FabricMaterialsDao;
import net.lab1024.sa.admin.module.fabricmaterials.domain.entity.FabricMaterialsEntity;
import net.lab1024.sa.admin.module.users.usage.dao.ConsumablesUsageDao;
import net.lab1024.sa.admin.module.users.usage.domain.entity.ConsumablesUsageEntity;
import net.lab1024.sa.admin.module.users.usage.domain.form.ApprovalFrom;
import net.lab1024.sa.admin.module.users.usage.domain.form.ConsumablesUsageAddForm;
import net.lab1024.sa.admin.module.users.usage.domain.form.ConsumablesUsageQueryForm;
import net.lab1024.sa.admin.module.users.usage.domain.form.ConsumablesUsageUpdateForm;
import net.lab1024.sa.admin.module.users.usage.domain.vo.ConsumablesUsageVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 实训室耗材领用 Service
 *
 * @Author 李韵妍
 * @Date 2024-11-08 13:31:03
 * @Copyright 🐂🐎bloc
 */

@Service
public class ConsumablesUsageService {
    private static final int agreed = 1;
    private static final int disAgreed = 2;
    private static final int pending = 0;
    @Resource
    private ConsumablesUsageDao consumablesUsageDao;

    @Resource
    private FabricMaterialsDao fabricMaterialsDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<ConsumablesUsageVO> queryPage(ConsumablesUsageQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ConsumablesUsageVO> list = consumablesUsageDao.queryPage(page, queryForm);
        PageResult<ConsumablesUsageVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(ConsumablesUsageAddForm addForm) {
        ConsumablesUsageEntity consumablesUsageEntity = SmartBeanUtil.copy(addForm, ConsumablesUsageEntity.class);
        consumablesUsageDao.insert(consumablesUsageEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(ConsumablesUsageUpdateForm updateForm) {
        ConsumablesUsageEntity consumablesUsageEntity = SmartBeanUtil.copy(updateForm, ConsumablesUsageEntity.class);
        consumablesUsageDao.updateById(consumablesUsageEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        consumablesUsageDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        consumablesUsageDao.deleteById(id);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> approval(ApprovalFrom approveForm) {
        ConsumablesUsageEntity consumablesUsageEntity = SmartBeanUtil.copy(approveForm, ConsumablesUsageEntity.class);
        if(approveForm.getWarehouseManager() != null && approveForm.getWarehouseManager() != pending){
            consumablesUsageEntity.setWarehouseManager(approveForm.getWarehouseManager());
        }
        if(approveForm.getTrainingDepartment()!= null && approveForm.getTrainingDepartment()!= pending){
            consumablesUsageEntity.setTrainingDepartment(approveForm.getTrainingDepartment());
        }

        if(approveForm.getTrainingDepartment()!=null && approveForm.getTrainingDepartment()!= pending && approveForm.getWarehouseManager()!= null && approveForm.getWarehouseManager()!= pending){
            Date date = new Date();
            String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
            Timestamp timestamp = Timestamp.valueOf(format);
            consumablesUsageEntity.setUsageDate(timestamp);
            FabricMaterialsEntity objFabricMaterials = fabricMaterialsDao.selectById(approveForm.getMaterialId());
            FabricMaterialsEntity fabricMaterialsEntity = new FabricMaterialsEntity();
            fabricMaterialsEntity.setId(approveForm.getMaterialId());
            int count = objFabricMaterials.getQuantity() - approveForm.getQuantity();
            if(count < 0){
                return ResponseDTO.ok("库存不足");
            }
            fabricMaterialsEntity.setQuantity(objFabricMaterials.getQuantity() - approveForm.getQuantity() );
            fabricMaterialsEntity.setOutStock(objFabricMaterials.getOutStock() + approveForm.getQuantity());
            fabricMaterialsDao.updateById(fabricMaterialsEntity);
        }
        consumablesUsageDao.updateById(consumablesUsageEntity);
        return ResponseDTO.ok();
    }
}
